package com.scwang.smart.drawer.exercise.model;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 练习题目列表
 * Created by SCWANG on 2018/1/8.
 */
public class Example {

    public String Id;//"练习 Id",
    public String ResId;//"习题或资源 Id"【用于评论】,
    public String Title;//"练习标题",

    public int Count;//题目数量,
    public int UseTime;//已用时（秒）,
    public boolean IsAllowRemark;//是否允许评论（true 允许,false 不允许（隐藏右上角的评论图标））,

    public List<Subject> Subjects;//题目列表
    public List<Card> AnswerCards;//答题卡

    /**
     * 非服务器返回数据
     */
    public List<Subject> SubjectGroups;//题目列表

    @NonNull
    public List<? extends Subject> getSubjects() {
        if (SubjectGroups == null) {
            Subject lastGroup = null;
            SubjectGroups = new ArrayList<>(Subjects.size());
            for (Subject subject: Subjects) {
                if (!TextUtils.isEmpty(subject.GroupContent) && subject.GroupType == 2) {
                    if (lastGroup == null || !TextUtils.equals(lastGroup.GroupContent, subject.GroupContent)) {
                        lastGroup = subject;
                        SubjectGroups.add(subject);
                    }
                    lastGroup.addSubject(subject);
                } else {
                    SubjectGroups.add(subject);
                }
            }
        }
        return SubjectGroups;
    }

    public Card getCardFromSubject(Subject subject) {
        if (AnswerCards != null) {
            for (int i = 0; i < AnswerCards.size(); i++) {
                if (TextUtils.equals(subject.Id, AnswerCards.get(i).Id)) {
                    return AnswerCards.get(i);
                }
            }
        }
        return null;
    }
}
